package com.lwq.commentTool.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileUtil {
	public static final int DATATYPE_STRING = 1;
	public static final int DATATYPE_BYTE = 2;
	public static final int DATATYPE_OUTPUTSTREAM = 3;
	
	public static final int FILEEND = -1;
	public static Object readFileData(String path,int start,int end,int returnType){
		File file = new File(path);
		String dataStr = null;
		long len = file.length();
		byte[] buf = null;
		FileInputStream inStream = null;
		if(!file.isFile()){
			return null;
		}
		if(len > 0){
			try{
				inStream = new FileInputStream(file);
				if(end == FILEEND){
					end = (int)len;
				}
				int readLen = end-start;
				if(returnType == DATATYPE_BYTE){
					buf = new byte[(int)readLen];
					inStream.read(buf,start,end);
				}else if(returnType == DATATYPE_STRING){
					buf = new byte[(int)readLen];
					inStream.read(buf,start,end);
					dataStr = new String(buf);
				}
			}
			catch(IOException e){
				e.printStackTrace();
				dataStr = null;
				buf = null;
				inStream = null;
			}
		}
		if(returnType == DATATYPE_STRING){
			return dataStr;
		}else if(returnType == DATATYPE_BYTE){
			return buf;
		}else if(returnType == DATATYPE_OUTPUTSTREAM){
			return inStream;
		}else{
			return null;
		}
	}
	
	public static boolean writeFileData(String path,String data){
		File file = new File(path);
		boolean state = false;
		if(!file.exists()){
			completeDirsExceptLastDir(path);
		}else{
			if(!file.isFile()){
				return false;
			}
		}

		try{
			FileOutputStream opStream = new FileOutputStream(file);
			opStream.write(data.getBytes());
			state = true;
		}
		catch(IOException e){
			e.printStackTrace();
			state = false;
		}
		return state;
	}
	
	public static boolean writeFileData(String path,byte[] data){
		File file = new File(path);
		boolean state = false;
		if(!file.isFile()){
			return false;
		}
		try{
			FileOutputStream opStream = new FileOutputStream(file);
			opStream.write(data);
			state = true;
		}
		catch(IOException e){
			e.printStackTrace();
			state = false;
		}
		return state;
	}
	
	public static String parseFileName (String path){
		String fileName = null;
		String[] tmpStr = null;
		String cutStr = null;
		if(path != null){
			if(path.contains("\\")){
				cutStr = "\\\\";
			}else if(path.contains("/")){
				cutStr = "/";
			}
			if(cutStr != null){
				tmpStr = path.split(cutStr);
				if(tmpStr.length > 0){
					fileName = tmpStr[tmpStr.length-1];
				}
			}
		}
		return fileName;
	}

	public static String parseDirName (String path){
		String dirName = null;
		String[] tmpStr = null;
		String cutStr = null;
		if(path != null){
			if(path.contains("\\")){
				cutStr = "\\\\";
			}else if(path.contains("/")){
				cutStr = "/";
			}
			if(cutStr != null){
				tmpStr = path.split(cutStr);
				int index = path.indexOf(tmpStr[tmpStr.length-1]) - cutStr.length();
				dirName = path.substring(0,index);
			}
		}
		return dirName;
	}

	public static boolean completeDirsExceptLastDir (String path){
		String dirPath = parseDirName(path);
		File file = new File(dirPath);
		return file.mkdirs();
	}

}
